import { getRandomElements, HostEnum, type GameJson } from "~/utils";
import { getServerCache } from "../plugins/cache";

/** 随机选择一个游戏 */
export default defineEventHandler(async (event): Promise<GameJson> => {
  const { hostName = HostEnum.localhost } = await readBody(event);
  const { gameJsonDataSource } = getServerCache();
  const { allGameJson } = gameJsonDataSource[hostName];
  const randomGameDetail = getRandomElements(allGameJson, 1)[0] as GameJson;
  return randomGameDetail;
});
